<?php
class Video{
	/**
	 * Enter description here...
	 *
	 * @var Mysql
	 */
	private $db;
	
	public function __construct($db)
	{
		$this->db = $db;
	}
	
	public function add( array $value)
	{
		$v = array();
		$v['name'] = $value['name'];
		$v['author'] = $value['author'];
		$v['image'] = $value['image'];
		$v['path'] = $value['path'];
		$v['content'] = $value['content'];
		$v['seq'] = $value['seq'];
		$v['url'] = $value['url'];
		$v['pubtime'] = $value['pubtime'];
		
		$mid = $this->db->insert("video", $v, true);
		$this->updateGroupCache();
		
		return $mid;
	}

	public function change($vid, array $value)
	{
		$v = array();
		isset($value['name']) && $v['name'] = $value['name'];
		isset($value['author']) && $v['author'] = $value['author'];
		isset($value['image']) && $v['image'] = $value['image'];
		isset($value['path']) && $v['path'] = $value['path'];
		isset($value['content']) && $v['content'] = $value['content'];
		isset($value['seq']) && $v['seq'] = $value['seq'];
		isset($value['pubtime']) && $v['pubtime'] = $value['pubtime'];
		isset($value['url']) && $v['url'] = $value['url'];

		$this->db->update("video", $v, 'vid=' . $vid);
		$this->updateGroupCache();
	}
	
	public function del($vid)
	{
		$this->db->delete("video", 'vid=' . $vid);
		$this->updateGroupCache();
	}
	
	
	/**
	 * 读取文章列表
	 *
	 * @param Integer $nowp
	 * @param Integer $per_page
	 */
	public function listvideo($nowp, $per_page)
	{		
		 $totalNum = $this->db->fetchOne ('select count(*) from video');
        
        require_once APP . '/application/models/Util.php';
        
        $pageInfo = Util::page ($totalNum, $per_page, $nowp);
        $start    = $pageInfo ['start'];
        $list     = $this->db->fetchAll ('select * from video order by seq desc,vid desc limit ' . $start . ',' . $per_page);
        
        return Array ('page' => $pageInfo['page'], 'list' => $list);
	}
	
	private function updateGroupCache()
	{
		
	}
	
	public function show($vid)
	{
		return $this->db->fetchRow("select * from video where vid=\"?\"", array($vid));
	}
}